<?php
namespace app\home\controller;
use think\Controller;
use think\Captcha;
use think\Db;

class User extends Controller
{
    //显示购物车，不做！
    public function adduser()
    {
        if (request()->isPost()) {
            $data = [
                'username' => input('username'),
                'userpass' => input(think_weiphp_md5('password')),
                'mobilenum' => input('telephone'),
                'email' => input('email'),
                'createtime'=>time(),
            ];
            $this->validate($data,[
                'captcha|验证码'=>'require|captcha'
            ]);

            if ($_FILES['pic']['tmp_name']) {
                $_FILES = request()->file('pic');
                $info = $_FILES->validate(['size' => 15678, 'exr' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . '/static/home/upload');
                if ($info) {
                    $data['pic'] = '/static/uploads/'.date('Ymd').'/'.$info->getFilename();
                } else {

                    echo $_FILES->getError();
                }
            }
       $validate = \think\Loader::validate('users');
        if($validate->check($data)) {
            $db = \think\Db::name('users')->insert();
            if ($db) {

                return $this->success('注册成功', 'userinfo');
            } else {
                return $this->error('注册失败!');
            }
        }else{
            return $this->error($validate->getError());
            }

        return;
    }
            return $this->fetch();
    }

    public function shoppingcart()
    {
        return $this->fetch();
    }

    //显示用户个人中心
    public function userinfo()
    {
        $user = \think\Db::name('users')->where('id',input('id'))->find();
        $this->assign('users',$user);
        return $this->fetch();
    }

    //显示修改个人资料
    public function editinfo()
    {
        $userinfo = \think\Db::name('users')->where('id',input("id"))->find();
        $this->assign('userinfo',$userinfo);
        return $this->fetch();
    }

    //更新个人资料
    public function doUpdate()
    {
        if(request()->isPost()){
            $data=[
                'id'=>input('id'),
                'username'=>input('username'),
                'nickname'=>input('nickname'),
                'email'=>input('email'),
                'mobilenum'=>input('mobilenum'),
                'address'=>input('address'),
            ];
            if($_FILES['pic']['tmp_name']){
                $_FILES = request()->file('pic');
                $info = $_FILES->validate(['size' => 15678, 'exr' => 'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . '/static/upload');
                if ($info) {
                   $data['pic'] = '/static/uploads/'.date('Ymd').'/'.$info->getFilename();
                } else {

                    echo $_FILES->getError();
                }

            }
            $volidate = \think\Loader::validate('users');
            if($volidate->check($data)){
                $db = \think\Db::name('users')->update($data);
                if($db){

                    return $this->success('修改成功','userinfo');
                }else{

                    return $this->error('修改失败');
                }

            }
        }
        return $this->fetch();
    }

    //显示订单列表
    public function orderlists()
    {

        return $this->fetch();
    }

    //显示评论列表
    public function commentlists()
    {
        return $this->fetch();
    }
}
